<?php

// Check if we are a user
OCP\User::checkLoggedIn();
OCP\JSON::callCheck();

// remove user's 
$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
$limit = 10; 
list($notification, $total) = DelayTaskJob::getTasks(OC_User::getUser(), $limitstart, $limit);

$data = array();
$unfinished = 0;
foreach($notification as $n) {
   $unfinished = $n->unfinished; 
   $data[] = array('id'=>$n->id, 
                   'created'=>JHtml::_('date',$n->created, 'Y-m-d H:i:s'),
                   'summary'=>$n->summary, 
                   'resulturi'=>$n->resulturi, 
                   'error'=>$n->error, 
                   'percentage'=>$n->status
                  );
}

// generate html pagination 
$pagination = new PPagination($total, $limitstart, $limit);

// if pagesTotal >=1 but we have no data, we are at the end of page 
$currentpage = $pagination->pagesCurrent-1; 
if($pagination->pagesTotal >=1 && empty($data)) {
   $currentpage = $pagination->pagesTotal; 
}

$pages = array('pagestotal'=>$pagination->pagesTotal, 
               'currentpage'=>$currentpage,
               'limit'=>$limit);

OC_JSON::success(array("data"=>$data, "unfinished"=>$unfinished, "pagination"=>$pages));
